import { Worker, isMainThread, workerData, parentPort } from 'worker_threads'
import {hash} from 'blake3'

do =>
  [difficulty, buf, n, step] = workerData

  token = Buffer.allocUnsafe 8
  n = BigInt n
  begin = n
  step = BigInt step

  while true
    token.writeBigInt64BE n
    if hash(
      Buffer.concat [token,buf]
    ).readUIntBE(0, difficulty) == 0
      parentPort.postMessage token
      return

    n += step

